package com.jk.controller;


import com.alibaba.fastjson.JSONObject;
import com.jk.bean.User;
import com.jk.client.LoginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Controller
@RequestMapping("login")
public class LoginController {

    @Resource
    private LoginService loginService;


    @RequestMapping("toView")
    public String viewName(String viewName,String id,HttpSession session){
          session.setAttribute("msg",id);
        return viewName;
    }

    @RequestMapping("login")
    public String toView(String login,String id,HttpSession session){
        session.setAttribute("msg",id);
        return login;
    }

    @ResponseBody
    @RequestMapping("userLogin")
    public boolean userLogin(HttpSession session, User user, HttpServletResponse response , HttpServletRequest request) throws UnsupportedEncodingException {
        User users = loginService.userLogin(user);

        if(users==null){
           return false;
        }

        if(user.getCheck()!=null) {

            //2.把user对象-->json字符串 !!!!!!!!!一定要实现
            String userText = JSONObject.toJSONString(user);
            //进行编码
            String encode = URLEncoder.encode(userText, "utf-8");

            Cookie pwd = new Cookie("pwd",encode);
            pwd.setMaxAge(86400);
            pwd.setPath("/");
            response.addCookie(pwd);

            Cookie cookie = new Cookie("login",users.getUserAccout()+"-"+user.getUserPwd());

            cookie.setMaxAge(86400);

            cookie.setPath("/");

            response.addCookie(cookie);
        }else{

            Cookie[] cookies = request.getCookies();
            if (null==cookies) {
                System.out.println("没有cookie==============");
            } else {
                for(Cookie cookie : cookies){
                    if(cookie.getName().equals("login")){
                        cookie.setValue(null);
                        cookie.setMaxAge(0);// 立即销毁cookie
                        cookie.setPath("/");
                        System.out.println("被删除的cookie名字为:"+cookie.getName());
                        response.addCookie(cookie);
                        break;
                    }
                }
            }

        }


            if(users!=null){
                session.setAttribute("hy",users.getHy());
                session.setAttribute("status",users.getStatus());
                session.setAttribute("msgUser","你好  "+users.getNickName()+"  ");
                session.setAttribute("user",users);
                return true;
            }

        return false;
    }


    //注销
    @RequestMapping("removeSession")
    public String removeSession(HttpSession session, HttpServletResponse response, HttpServletRequest request)  {

        Cookie[] cookies = request.getCookies();

        try {
            for (Cookie cookie : cookies) {
                if(cookie.getName().equals("login")){
                  continue;
                }else {
                    cookie.setValue(null);
                    cookie.setMaxAge(0);// 立即销毁cookie
                    cookie.setPath("/");
                    System.out.println("被删除的cookie名字为:"+cookie.getName());
                    response.addCookie(cookie);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        session.invalidate();
        return "login";
    }


}
